<?php
require_once(dirname(__FILE__).'/../../../WildPack/WildPack.php');
$wp = new WildPack();
$wp->using('util');
$wp->using('dataaccess.wpdatex');

require_once(dirname(__FILE__).'/Tester.php');
require_once(dirname(__FILE__).'/TesterView.php');

class TestSimpleDatexV extends WpTestSuite
{
  private $_host = 'localhost';
  private $_user = 'wp';
  private $_password = 'wp';
  private $_db = 'wp';
  private $_table1 = 'tester';
  private $_table2 = 'tester_add_data';
  
  /**
   * Preparation
   */
  public function prepare()
  {   
    $this->addMessage('Using database host: '.$this->_host);
    $this->addMessage('Using database: '.$this->_db);
    $this->addMessage('Using user: '.$this->_user);
    $this->addMessage('Using database table: '.$this->_table);
    
    $this->addMessage('Connect to database...');
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      return false;
    }
    $this->addMessage('... established!');
    
    $this->addMessage('Has database?');
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('Database not found. Try creating...');      
      if (!mysql_query('CREATE DATABASE '.$this->_db)) {
        $this->addMessage('... not successfull. Test stopped.');
        return false;
      } else {
        $this->addMessage('... successfull.');
        mysql_select_db($this->_db, $c);    
      }
    } else {
      $this->addMessage('Database found'); 
    }
    
    $r = mysql_query('SHOW TABLES');
    $found1 = false;
    $found2 = false;
    while ($row = mysql_fetch_array($r)) {
      if ($row[0] === $this->_table1) {
        $found1=true;
      }
      if ($row[0] === $this->_table2) {
        $found2=true;
      }
      
      if ($found1 === true && $found2 === true) {
        break;
      } 
    }
    mysql_free_result($result);
    
    if (!$found1) {
      if (!mysql_query('CREATE TABLE  `'.$this->_table1.'` ('.
        'id int(11) NOT NULL AUTO_INCREMENT,'.
        'field1 varchar(255) NOT NULL,'.
        'field2 double NULL,'.
        'field3 text NULL,'.
        'PRIMARY KEY (id)'.
        ') ENGINE=MyISAM DEFAULT CHARSET=latin1'
      )) {
        $this->addMessage('Table1 not found. Try to create ... failed. Test stopped.');
        return false;
      } else {
        $this->addMessage('Table1 not found. Try to create ... successfull.');
      }
    } else {
      $this->addMessage('Table1 found.');
    }
    
    if (!$found2) {
      if (!mysql_query('CREATE TABLE  `'.$this->_table2.'` ('.
        'id int(11) NOT NULL AUTO_INCREMENT,'.
        'tester_id int(11) NOT NULL,'.
        'field2 varchar(255) NULL,'.
        'PRIMARY KEY (id)'.
        ') ENGINE=MyISAM DEFAULT CHARSET=latin1'
      )) {
        $this->addMessage('Table2 not found. Try to create ... failed. Test stopped.');
        return false;
      } else {
        $this->addMessage('Table2 not found. Try to create ... successfull.');
      }
    } else {
      $this->addMessage('Table2 found.');
    }
    
    if (!mysql_query('TRUNCATE '.$this->_table1)) {
      $this->addMessage('Trying to empty the table1 ... failed. Test stopped.');
      return false;
    }
    if (!mysql_query('TRUNCATE '.$this->_table2)) {
      $this->addMessage('Trying to empty the table2 ... failed. Test stopped.');
      return false;
    }

    
    $this->addMessage('Trying to empty the table1 ... successfull.');
    $this->addMessage('Trying to empty the table2 ... successfull.');
    return true;
  }
  
  /**
   * Test
   */
  public function test()
  {
    /* Single requests */
    $l = new WpMemLog();
    
    $c = new WpConnectMySQL(array(
      'location' => $this->_host.'/'.$this->_db,
      'user' => $this->_user,
      'password' => $this->_password,
      'log' => $l
    ));
    
    $this->addMessage('Create some datasets');
    $o = new Tester($c, $l);
    
    for ($i = 0; $i < 10; $i++) {
      $o->reset();
      $o->qset(array(
        'field1' => 'Dataset'.strval($i),
        'field2' => $i + 0.1,
        'field3' => 'Body'.strval($i)
      ));
      $o->save();
      
      for ($j = 0; $j <= $i; $j++) {
        $c->insert($this->_table2, array(
          'tester_id' => $o->get('id'),
          'field2' => 'Additional data'.strval($i).'_'.strval($j)
        ));
      }
    }
    
    $this->addMessage('Load all');
    $v = new TesterView($c, $l);
    $this->addResult('Load all', 55, $v->load(array(), 't_field2', 'ASC'), '===');
    $this->addResult('Loaded?', true, $v->Loaded, '===');
    $this->addResult('length', 55, $v->length(), '===');
    
    $v->rewind();
    $this->addResult('dataset0 findNext', true, $v->findNext(), '===');
    $this->addResult('dataset0 - t_field1', 'Dataset0', $v->get('t_field1'), '===');
    $this->addResult('dataset0 - t_field2', 0.1, $v->get('t_field2'), '===');
    $this->addResult('dataset0 - t_field3', 'Body0', $v->get('t_field3'), '===');
    $this->addResult('dataset0 - a_field2', 'Additional data0_0', $v->get('a_field2'), '===');
    $this->addResult('dataset1 findNext', true, $v->findNext(), '===');
    $this->addResult('dataset1 - t_field1', 'Dataset1', $v->get('t_field1'), '===');
    $this->addResult('dataset1 - t_field2', 1.1, $v->get('t_field2'), '===');
    $this->addResult('dataset1 - t_field3', 'Body1', $v->get('t_field3'), '===');
    $this->addResult('dataset1 - a_field2', 'Additional data1_0', $v->get('a_field2'), '===');
    $this->addResult('dataset2 findNext', true, $v->findNext(), '===');
    $this->addResult('dataset2 - t_field1', 'Dataset1', $v->get('t_field1'), '===');
    $this->addResult('dataset2 - t_field2', 1.1, $v->get('t_field2'), '===');
    $this->addResult('dataset2 - t_field3', 'Body1', $v->get('t_field3'), '===');
    $this->addResult('dataset2 - a_field2', 'Additional data1_1', $v->get('a_field2'), '===');
    
    $this->addMessage('Load with condition');
    $this->addResult('Load all', 19, $v->load(array('>>tester.field2' => 8), 't_field2', 'DESC'), '===');
   
    $this->addResult('Loaded?', true, $v->Loaded, '===');
    $this->addResult('length', 19, $v->length(), '===');
    $v->rewind();
    $this->addResult('dataset9_0 findNext', true, $v->findNext(), '===');
    $this->addResult('dataset9_0 - t_field1', 'Dataset9', $v->get('t_field1'), '===');
    $this->addResult('dataset9_0 - t_field2', 9.1, $v->get('t_field2'), '===');
    $this->addResult('dataset9_0 - t_field3', 'Body9', $v->get('t_field3'), '===');
    $this->addResult('dataset9_0 - a_field2', 'Additional data9_0', $v->get('a_field2'), '===');
    $this->addResult('dataset9_1 findNext', true, $v->findNext(), '===');
    $this->addResult('dataset9_1 - t_field1', 'Dataset9', $v->get('t_field1'), '===');
    $this->addResult('dataset9_1 - t_field2', 9.1, $v->get('t_field2'), '===');
    $this->addResult('dataset9_1 - t_field3', 'Body9', $v->get('t_field3'), '===');
    $this->addResult('dataset9_1 - a_field2', 'Additional data9_1', $v->get('a_field2'), '===');
    
    $this->addResult('dataset8_0 findNext', true, $v->findNext('t_field1', 'Dataset8'), '===');
    $this->addResult('dataset8_0 - t_field1', 'Dataset8', $v->get('t_field1'), '===');
    $this->addResult('dataset8_0 - t_field2', 8.1, $v->get('t_field2'), '===');
    $this->addResult('dataset8_0 - t_field3', 'Body8', $v->get('t_field3'), '===');
    $this->addResult('dataset8_0 - a_field2', 'Additional data8_0', $v->get('a_field2'), '===');
    $this->addResult('dataset8_1 findNext', true, $v->findNext(), '===');
    $this->addResult('dataset8_1 - t_field1', 'Dataset8', $v->get('t_field1'), '===');
    $this->addResult('dataset8_1 - t_field2', 8.1, $v->get('t_field2'), '===');
    $this->addResult('dataset8_1 - t_field3', 'Body8', $v->get('t_field3'), '===');
    $this->addResult('dataset8_1 - a_field2', 'Additional data8_1', $v->get('a_field2'), '===');
  }

  
  /**
   * Cleanup
   */
  public function cleanup()
  {
    $c = mysql_connect($this->_host, $this->_user, $this->_password);
    if (!$c) {
      $this->addMessage('Connection failed. Test stopped.');
      return;
    }
    if (!@mysql_select_db($this->_db, $c)) {
      $this->addMessage('DB selection failed. Test stopped.');
      return;
    }
    $r = mysql_query('TRUNCATE '.$this->_table1);
    if (!$r) {
      $this->addMessage('Table1 could not be truncated.');
    } else {
      $this->addMessage('Successfully truncated table1.');
    }
    $r = mysql_query('TRUNCATE '.$this->_table2);
    if (!$r) {
      $this->addMessage('Table2 could not be truncated.');
    } else {
      $this->addMessage('Successfully truncated table2.');
    }
  }
  
  
}

$t = new TestSimpleDatexV();
$t->start('SimpleDatexV');
